/**
 * 谢泽鹏：自定义扩展
 * 扩展easyui中tabs的部分方法，
 * 实现根据唯一标识id的进行相应操作；
 * 技巧：使用一个自执行的函数，激发作用域，避免这里定义的变量与系统全局变量冲突.
 */
(function(){
    var _methods = $.fn.tabs.methods;
    //保存原方法
    var _exists = _methods.exists;
    //保存原方法
    var _getTab = _methods.getTab;
    $.extend($.fn.tabs.methods, {
        //重写getTab方法，增加根据id获取tab（注意：这里我们可以定义任意的获取方式，不必一定使用id）
        getTab : function(jq, which) {
            if(!which)
                return null;
            var tabs = jq.data('tabs').tabs;
            for (var i = 0; i < tabs.length; i++) {
                var tab = tabs[i];
                if (tab.panel("options").id == which) {
                    return tab;
                }
            }
            //如果根据id无法获取，则通过easyui默认的getTab进行获取
            return _getTab(jq, which);
        },
        //重写exists方法，增加id判断
        exists : function(jq, which) {
            //调用重写后的getTab方法
            return this.getTab(jq, which)!= null;
        }
    });
        //扩展easyui tree的搜索方法
    $.extend($.fn.tree.methods, {

        search: function(jqTree, searchText) {
            // easyui tree的tree对象。可以通过tree.methodName(jqTree)方式调用easyui tree的方法
            var tree = this;
            // 获取所有的树节点
            var nodeList = getAllNodes(jqTree, tree);
            // 如果没有搜索条件，则展示所有树节点
            searchText = $.trim(searchText);
            if (searchText == "") {
                for (var i=0; i<nodeList.length; i++){
                    $(".tree-node-targeted", nodeList[i].target).removeClass("tree-node-targeted");
                    $(nodeList[i].target).show();
                }
                // 展开已选择的节点（如果之前选择了）
                var selectedNode = tree.getSelected(jqTree);
                if (selectedNode) {
                    tree.expandTo(jqTree, selectedNode.target);
                }
                return;
            }
            // 搜索匹配的节点并高亮显示
            var matchedNodeList = [];
            if (nodeList && nodeList.length>0) {
                var node = null;
                for (var i=0; i<nodeList.length; i++){
                    node = nodeList[i];
                    if (isMatch(searchText, node.text)) {
                        matchedNodeList.push(node);
                    }
                }
                // 隐藏所有节点
                for (var i=0; i<nodeList.length; i++){
                    $(".tree-node-targeted", nodeList[i].target).removeClass("tree-node-targeted");
                    $(nodeList[i].target).hide();
                }
                // 折叠所有节点
                tree.collapseAll(jqTree);
                // 展示所有匹配的节点以及父节点
                for (var i=0; i<matchedNodeList.length; i++){
                    showMatchedNode(jqTree, tree, matchedNodeList[i]);
                }
            }
        },
        showChildren: function(jqTree, node) {
            // easyui tree的tree对象。可以通过tree.methodName(jqTree)方式调用easyui tree的方法
            var tree = this;
            // 展示子节点
            if (!tree.isLeaf(jqTree, node.target)) {
                var children = tree.getChildren(jqTree, node.target);
                if (children && children.length>0) {
                    for (var i=0; i<nodeList.length; i++){
                        if ($(children[i].target).is(":hidden")) {
                            $(children[i].target).show();
                        }
                    }
                }
            }
        },
        scrollTo:function(jqTree, param) {
            // easyui tree的tree对象。可以通过tree.methodName(jqTree)方式调用easyui tree的方法
            var tree = this;
            // 如果node为空，则获取当前选中的node
            var targetNode = param && param.targetNode ? param.targetNode : tree.getSelected(jqTree);
            if (targetNode != null) {
                // 判断节点是否在可视区域
                var root = tree.getRoot(jqTree);
                var $targetNode = $(targetNode.target);
                var container = param && param.treeContainer ? param.treeContainer : jqTree.parent();
                var containerH = container.height();
                var nodeOffsetHeight = $targetNode.offset().top - container.offset().top;
                if (nodeOffsetHeight > (containerH - 30)) {
                    var scrollHeight = container.scrollTop() + nodeOffsetHeight - containerH + 30;
                    container.scrollTop(scrollHeight);
                }
            }
        }
    });
    function showMatchedNode(jqTree, tree, node) {
        // 展示所有父节点
        $(node.target).show();
        $(".tree-title", node.target).addClass("tree-node-targeted");
        var pNode = node;
        while ((pNode = tree.getParent(jqTree, pNode.target))) {
            $(pNode.target).show();
        }
        // 展开到该节点
        tree.expandTo(jqTree, node.target);
        // 如果是非叶子节点，需折叠该节点的所有子节点
        if (!tree.isLeaf(jqTree, node.target)) {
            tree.collapse(jqTree, node.target);
        }
    }
    function isMatch(searchText, targetText) {
        return $.trim(targetText)!="" && targetText.indexOf(searchText)!=-1;
    }
    function getAllNodes(jqTree, tree) {
        var allNodeList = jqTree.data("allNodeList");
        if (!allNodeList) {
            var roots = tree.getRoots(jqTree);
            allNodeList = getChildNodeList(jqTree, tree, roots);
            jqTree.data("allNodeList", allNodeList);
        }
        return allNodeList;
    }
    function getChildNodeList(jqTree, tree, nodes) {
        var childNodeList = [];
        if (nodes && nodes.length>0) {
            var node = null;
            for (var i=0; i<nodes.length; i++){
                node = nodes[i];
                childNodeList.push(node);
                if (!tree.isLeaf(jqTree, node.target)) {
                    var children = tree.getChildren(jqTree, node.target);
                    childNodeList = childNodeList.concat(getChildNodeList(jqTree, tree, children));
                }
            }
        }
        return childNodeList;
    }
})()
